/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biness.product.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.biness.product.domain.THfProductDetail;
import me.zhengjie.modules.biness.product.repository.THfProductDetailRepository;
import me.zhengjie.modules.biness.product.service.THfProductDetailService;
import me.zhengjie.modules.biness.product.service.dto.THfProductDetailDto;
import me.zhengjie.modules.biness.product.service.dto.THfProductDetailQueryCriteria;
import me.zhengjie.modules.biness.product.service.mapstruct.THfProductDetailMapper;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author yanzz
* @date 2021-02-18
**/
@Service
@RequiredArgsConstructor
public class THfProductDetailServiceImpl implements THfProductDetailService {

    private final THfProductDetailRepository tHfProductDetailRepository;
    private final THfProductDetailMapper tHfProductDetailMapper;

    @Override
    public Map<String,Object> queryAll(THfProductDetailQueryCriteria criteria, Pageable pageable){
        Page<THfProductDetail> page = tHfProductDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(tHfProductDetailMapper::toDto));
    }

    @Override
    public List<THfProductDetailDto> queryAll(THfProductDetailQueryCriteria criteria){
        return tHfProductDetailMapper.toDto(tHfProductDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public THfProductDetailDto findById(Integer id) {
        THfProductDetail tHfProductDetail = tHfProductDetailRepository.findById(id).orElseGet(THfProductDetail::new);
        ValidationUtil.isNull(tHfProductDetail.getId(),"THfProductDetail","id",id);
        return tHfProductDetailMapper.toDto(tHfProductDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public THfProductDetailDto create(THfProductDetail resources) {
        return tHfProductDetailMapper.toDto(tHfProductDetailRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(THfProductDetail resources) {
        THfProductDetail tHfProductDetail = tHfProductDetailRepository.findById(resources.getId()).orElseGet(THfProductDetail::new);
        ValidationUtil.isNull( tHfProductDetail.getId(),"THfProductDetail","id",resources.getId());
        tHfProductDetail.copy(resources);
        tHfProductDetailRepository.save(tHfProductDetail);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            tHfProductDetailRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<THfProductDetailDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (THfProductDetailDto tHfProductDetail : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("年级", tHfProductDetail.getLevel());
            map.put("A级课程", tHfProductDetail.getBestCourse());
            map.put("1对1", tHfProductDetail.getOneToOne());
            map.put("1对2", tHfProductDetail.getOneToTwo());
            map.put("3-6班组", tHfProductDetail.getThreeToSix());
            map.put("7-12班组", tHfProductDetail.getSevenToMax());
            map.put("价格版本号", tHfProductDetail.getCostVersion());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}